package com.gzxx.domain.bo;

import com.gzxx.domain.base.RequestPage;
import com.gzxx.domain.po.SysDept;
import com.gzxx.util.SysLoginUserHelper;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.util.Assert;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * SysDeptBO
 *
 * @author chenjing
 * @date 2020-09-22 17:43:19
 */
@Builder
@Getter
@ToString
public class SysDeptBO {

    /**
     * id
     */
    private Long id;
    /**
     * 部门名称
     */
    private String name;
    /**
     * 部门等级
     */
    private Integer level;
    /**
     * 上级ID
     */
    private Long parentId;
    /**
     * 部门状态
     */
    private Integer status;
    /**
     * 创建人
     */
    private Long createBy;
    /**
     * 更新人
     */
    private Long updateBy;
    /**
     * 分页
     */
    private RequestPage page;

    public Map<?, ?> queryParams() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return BeanMap.create(this);
    }

    /**
     * 创建
     */
    public SysDept persist() {
        return SysDept.builder()
                .name(this.name)
                .level(this.level)
                .parentId(this.parentId)
                .createBy(SysLoginUserHelper.getLoginUserId())
                .build();
    }


    /**
     * 编辑
     */
    public SysDept edit() {
        return SysDept.builder()
                .id(this.id)
                .name(this.name)
                .level(this.level)
                .updateBy(SysLoginUserHelper.getLoginUserId())
                .build();
    }

    /**
     * 更改状态
     */
    public SysDept changeStatus() {
        Assert.state(this.id > 0L, "SysDept must provide update identifier.");
        return SysDept.builder()
                .id(this.id)
                .status(this.status)
                .updateBy(SysLoginUserHelper.getLoginUserId())
                .build();
    }
}
